package com.repeat.member.service;

import com.repeat.member.model.UserSignRecords;
import com.repeat.member.model.UserSignStatus;
import com.repeat.member.mq.produc.UserPointsProduct;
import com.repeat.member.repository.UserRepository;
import com.repeat.member.repository.UserSignRecordsRepository;
import com.repeat.member.repository.UserSignStatusRepository;

import com.shop.common.constant.RabbitFinalConstant;
import com.shop.common.enums.ExceptionEnum;
import com.shop.common.enums.PointsConstant;
import com.shop.common.exception.OperationException;
import com.shop.servlet.utils.UserSecurity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;

import java.time.ZoneId;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class UserDetailsServiceImpl {//implements UserDetailsService

    @Autowired
    private UserSignRecordsRepository userSignRecordsRepository;
    @Autowired
    private UserSignStatusRepository userSignStatusRepository;

    @Autowired
    private UserPointsProduct userPointsProduct;


    public boolean checkIn(){
        LocalDateTime systemTime = LocalDateTime.now(ZoneId.systemDefault());
        System.out.println("系统时区时间: " + systemTime);
        Long user_id = UserSecurity.getUserId();
        long startTime = System.currentTimeMillis();
        LocalDate today = LocalDate.now();
        log.info("user_id {} {} {}",user_id,startTime,today);
        //今天是否签到
       Boolean is_check = userSignRecordsRepository.existsByUserIdAndSignDate(user_id,today);
       if(is_check){
           throw new OperationException(ExceptionEnum.SIGNED_IN_TODAY);
       }
       //最近签到状态
        UserSignStatus userSignStatus =  userSignStatusRepository.findById(user_id).orElse(null);
        int continuousDays = 1;
        UserSignStatus signStatus = new UserSignStatus();
       if(userSignStatus != null){
           //签到最后一天加一天
          LocalDate new_day = userSignStatus.getLastSignDate().plusDays(1);
           if(new_day.equals(today)){ //是否是今天
               continuousDays  = userSignStatus.getContinuousDays()+1;
           }
       }
        signStatus.setUserId(user_id);
        signStatus.setLastSignDate(today);
        signStatus.setContinuousDays(continuousDays);
        signStatus.setUpdatedAt(LocalDateTime.now());
        userSignStatusRepository.save(signStatus);
       //添加记录
        UserSignRecords userSignRecords = new UserSignRecords();
        userSignRecords.setUserId(user_id);
        userSignRecords.setSignDate(today);
        userSignRecords.setContinuousDays(continuousDays);
        userSignRecords.setCreatedAt(LocalDateTime.now());
        userSignRecordsRepository.save(userSignRecords);
        log.info("userSignStatus {} {} {}",userSignStatus,continuousDays,LocalDateTime.now());
        //签到发送消息
        Map<String,Object> map = new HashMap<>();
        map.put("userId",user_id);
        map.put("points",100);
        map.put("RecordId",userSignRecords.getId());
        map.put("RewardType", PointsConstant.SignType.getCode());
        userPointsProduct.SendPointsMsg(RabbitFinalConstant.POINTS_EXCHANGE,RabbitFinalConstant.SING_ROUTING_KEY,map);
        return true;
    }
}
